#include <poll.h>
#include <unistd.h>
#include <iostream>

int main(){
  //poll(struct pollfd *__fds, nfds_t nfds, int timeout)
  int max_fd = 1;
  struct pollfd fds[10];
  fds[0].fd = 0;
  fds[0].events = POLLIN;
  fds[1].fd = 1;
  fds[1].events = POLLIN | POLLERR;

  while(1){
    int ret = poll(fds, max_fd + 1, 500);
    if(ret < 0){
      perror("poll");
      return 0;
    }
    if(ret == 0){
      printf("poll timeout\n");
    }
    for(int i = 0; i < max_fd; ++i){
      if(fds[i].revents == POLLIN){
        char buf[1024] = {0};
        read(fds[i].fd, buf, sizeof(buf) - 1);
        printf("[%d]:%s", i, buf);
      }
    }
  }
  return 0;
}
